package com.fatgirl.utils
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	
	/**
	 *  
	 * sprite代理器
	 * @author iceman
	 * 
	 */
	public class SpriteProxy extends Sprite
	{
		
		private var m_url:String;
		private var m_data:Object;
		private var m_progress:int;
		private var m_loadResultType:int;

		private var m_LoadingDisObj:DisplayObject;
		private var m_ErrorImg:BitmapData;
		
		private var m_width:Number = -1;
		private var m_height:Number = -1;
		public function SpriteProxy()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE,addedHandler);
		}
		/**
		 * 获取显示对象数据  
		 * @return 
		 * 
		 */
		public function get data():Object
		{
			return m_data;
		}
		/**
		 * 设置显示对象数据 
		 * @param _obj
		 * 
		 */
		public function set data(_obj:Object):void
		{
			m_data = _obj;
			if(!m_data)
			{
				error();
			}
			else
			{
				clear();
				var t_obj:Object = m_data;
				if(t_obj is Bitmap)
				{
					var t_bitmap:Bitmap = new Bitmap((t_obj as Bitmap).bitmapData);
					addChild(t_bitmap);
				}
				else if(t_obj is DisplayObject)
				{
					var t_disObj:DisplayObject = t_obj as DisplayObject;
					addChild(t_disObj);
				}
				else if(t_obj is BitmapData)
				{
					t_bitmap = new Bitmap(t_obj as BitmapData);
					addChild(t_bitmap);
				}
				else
				{
					error();
				}
			}
			if(m_width!=-1)
			{
				this.width = m_width;
			}
			if(m_height!=-1)
			{
				this.height = m_height;
			}
		}
		/**
		 * 设置加载中的状态 
		 * @param _disObj
		 * 
		 */
		public function setLoadingSprite(_disObj:DisplayObject):void
		{
			m_LoadingDisObj = _disObj;
			if(m_LoadingDisObj)
			{
				clear();
				addChild(m_LoadingDisObj);
			}
		}
		/**
		 * 加载出错位图 
		 * @param _bitmapData
		 * 
		 */
		public function setErrorImg(_bitmapData:BitmapData):void
		{
			m_ErrorImg = _bitmapData;
		}
		public override function set width(value:Number):void
		{
			m_width = value;
			super.width = value;
		}
		public override function set height(value:Number):void
		{
			m_height = value;
			super.height = value;
		}
		private function addedHandler(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,addedHandler);
		}
		private function clear():void
		{
			//移除所有
			fremoveAllChild(this);
		}
		private function error():void
		{
			if(!m_ErrorImg) m_ErrorImg = new BitmapData(10,10,false,0xff0000);
			var t_errorBit:Bitmap = new Bitmap(m_ErrorImg);
			addChild(t_errorBit);
		}
	}
}